package com.abel.bigwater.data.sanchuan

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.data.mapper.DataMapper
import com.abel.bigwater.data.mapper.RtuMapper
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner


@SpringBootTest
@RunWith(SpringRunner::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class WeekDetailReplyTcpHandlerTests {
    val firmId = "99"
    val firmCode = "TS-"

    @Autowired
    var dataMapper: DataMapper? = null

    @Autowired
    var rtuMapper: RtuMapper? = null

    @Test
    fun test01Forward() {
        val str = """68 00 2c 68 12 02 10 11 02 03 04 05 36 01 03 20 18 08 02 06 00 01 06 02 00 00 00 0c 00 00 01 54
00 00 02 30 00 00 03 0c 00 00 03 84 00 00 03 ea e4 16"""

        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val ch = EmbeddedChannel(SanchuanDecoder(), SanchuanTcpHandler(dataMapper!!, rtuMapper!!, firmId, firmCode))
                .writeInbound(buf)

        Thread.sleep(2000)
    }

    @Test
    fun test02Pressure() {
        val str = """68 00 20 68 12 02 10 11 02 03 04 05 36 01 05 20 18 08 02 06 00 01 06 00 00 1e 00 1f 00 20 00 21
00 20 00 1f 8b 16"""

        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val ch = EmbeddedChannel(SanchuanDecoder(), SanchuanTcpHandler(dataMapper!!, rtuMapper!!, firmId, firmCode))
                .writeInbound(buf)

        Thread.sleep(2000)
    }

    @Test
    fun test03All() {
        val str = """68 00 71 68 12 02 10 11 02 03 04 05 36 01 07 20 18 08 02 06 00 01 06 02 00 00 00 0c 00 00 01 54
00 00 02 30 00 00 03 0c 00 00 03 84 00 00 03 ea 02 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00
05 00 00 00 06 00 00 00 07 00 00 1e 00 1f 00 20 00 21 00 20 00 1f 02 01 02 03 04 05 01 02 03 04
05 01 02 03 04 05 01 02 03 04 05 01 02 03 04 05 01 02 03 04 05 1e 16"""

        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val ch = EmbeddedChannel(SanchuanDecoder(), SanchuanTcpHandler(dataMapper!!, rtuMapper!!, firmId, firmCode))
                .writeInbound(buf)

        Thread.sleep(2000)
    }
}